package com.nice.meet_tool.agent.printsql;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.ReflectUtil;
import net.bytebuddy.implementation.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

/**
 * @author zhoujunlin
 * @date 2024/4/1 22:43
 * @desc
 */
public class MonitorPreparedStatementMethod {

    @RuntimeType
    public static Object intercept(@This Object obj, @Origin Method method, @SuperCall Callable<?> callable, @AllArguments Object... args) throws Exception {
        StopWatch stopWatch = new StopWatch();
        try {
            stopWatch.start();
            return callable.call();
        } finally {
            stopWatch.stop();
            String replaceSql = ReflectUtil.invoke(obj, "asSql");
            System.out.println("真实SQL：\r\n" + replaceSql);
            System.out.println("执行时长：" + stopWatch.getTotalTimeMillis() + "ms");
        }
    }

}
